#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>

#include "Log.hpp"

const std::string dictname = "./dict.txt";
const std::string sep = ":";

static bool Split(std::string &line, std::string &key, std::string &value)
{
    auto pos = line.find(sep);
    if(pos != std::string::npos)
    {
        key = line.substr(0, pos);
        value = line.substr(pos+sep.size());
        return true;
    }
    return false;
}

class Dict
{
private:
    std::unordered_map<std::string, std::string> dict;
public:
    Dict()
    {
        std::ifstream in(dictname);
        if(!in.is_open()){
            lg(Fatal, "ifstream open %s error...", dictname.c_str());
            exit(1);
        }
        // 打开文件成功
        std::string line;
        while(std::getline(in, line))
        {
            std::string part1, part2;
            Split(line, part1, part2);    // 分离key-value
            dict.insert({part1,part2});
        }
        in.close();
    }
    ~Dict()
    {}
public:
    std::string translation(const std::string &key)
    {
        auto iter = dict.find(key);
        if(iter != dict.end()){
            return iter->second;
        }
        else{
            return "UnKnow";
        }
    }
};

